import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:lib_core/customization/flutter/cupertino/text_selection.dart';
import 'package:lib_uikit/dimen/gt_dimensions_v3.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/widgets/text/gt_text.dart';

///星星图片大小：large20；medium16；small12
enum GTRatingBarSizeV3 { large, medium, small }

enum GTRatingBarTypeV3 { blue, red }

///Rate评分
class GTRatingBarV3 extends StatefulWidget {
  //星星数量，默认5个
  final int starCount;

  //默认选中星星数量
  final double rating;

  //点击星星回调
  final ValueChanged<double>? onRatingChanged;

  //星星右侧评分文字
  final String? ratingText;
  final GTRatingBarSizeV3? size;
  final GTRatingBarTypeV3? type;

  const GTRatingBarV3({
    super.key,
    this.starCount = 5,
    this.rating = 0.0,
    this.onRatingChanged,
    this.size,
    this.type,
    this.ratingText,
  });

  @override
  State<StatefulWidget> createState() => _RatingBarStateV3();
}

class _RatingBarStateV3 extends State<GTRatingBarV3> {
  double rating = 0.0, space = 8;
  late List<Color> _color;
  late GTRatingBarSizeV3 _size;
  late double starSize;

  @override
  void initState() {
    super.initState();
    rating = widget.rating;
    _size = widget.size ?? GTRatingBarSizeV3.medium;
    switch (_size) {
      case GTRatingBarSizeV3.large:
        starSize = 20;
        break;
      case GTRatingBarSizeV3.medium:
        starSize = 16;
        break;
      case GTRatingBarSizeV3.small:
        space = 2;
        starSize = 12;
        break;
    }

    switch (widget.type ?? GTRatingBarTypeV3.blue) {
      case GTRatingBarTypeV3.blue:
        _color = [GTColorsV3.icon5.color, GTColorsV3.icon5.color];
        break;
      case GTRatingBarTypeV3.red:
        _color = [GTColorsV3.gradient6Start.color, GTColorsV3.gradient6End.color];
        break;
    }
  }

  Widget buildStar(int index) {
    return GestureDetector(
      onTap: () {
        if (widget.onRatingChanged != null) {
          widget.onRatingChanged!(index + 1.0);
        }
        setState(() {
          rating = index + 1.0;
        });
      },
      child: Padding(
        padding: GTEdgeInsetsOnly(left: index > 0 ? space : 0),
        child: ShaderMask(
          shaderCallback: (Rect bounds) {
            return LinearGradient(
              begin: Alignment.centerLeft,
              end: Alignment.centerRight,
              colors: index < rating ? _color : [GTColorsV3.icon4.color, GTColorsV3.icon4.color],
            ).createShader(bounds);
          },
          blendMode: BlendMode.srcIn,
          child: Icon(
            GTIconFont.e97c,
            color: Colors.white,
            size: starSize,
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Row(
          mainAxisSize: MainAxisSize.min,
          children: List.generate(widget.starCount, (index) => buildStar(index)),
        ),
        if (widget.ratingText != null) ...[
          const SizedBox(
            width: 2,
          ),
          ShaderMask(
            shaderCallback: (Rect bounds) {
              return LinearGradient(
                begin: Alignment.centerLeft,
                end: Alignment.centerRight,
                colors: _color,
              ).createShader(bounds);
            },
            blendMode: BlendMode.srcIn,
            child: Padding(
              padding: const EdgeInsets.only(top: 2),
              child:GTText(
                widget.ratingText ?? "",
                removeTopBottomPadding: true,
                style: gtTextStyleW500(fontSize: 12),
              ) ,
            ),

          ),
        ]
      ],
    );
  }
}
